<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button v-if="$AUTH('systemLevel:add')" type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input v-model="search.typeName" placeholder="名称" clearable></el-input>
		  <el-input v-model="search.sysCode" placeholder="名称" clearable></el-input>
          <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="systemLevelId" @selection-change="selectionChange" stripe>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="NO" prop="systemLevelId" width="60"></el-table-column>
        <el-table-column label="名称" prop="typeName" width="150"></el-table-column>
		<el-table-column label="编码" prop="sysCode" width="150"></el-table-column>
        <el-table-column label="说明" prop="msg" width="150"></el-table-column>
		<el-table-column label="等级分类" width="550">
			<template #default="scope">
				<el-space v-for="item in scope.row.systemLevelTypes" :key="item.systemLevelBranchId">
					<el-button type="text" size="small" @click="level_type_edit(item)">{{item.typeName}}</el-button>
					<el-divider direction="vertical"></el-divider>
				</el-space>
				<el-button type="text" size="small" @click="level_type_add(scope.row.systemLevelId,scope.row.typeName)">添加分类</el-button>
			</template>
		</el-table-column>
        <el-table-column label="操作" fixed="right" align="right">
          <template #default="scope">
            <el-button v-if="$AUTH('systemLevel:add')" type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button v-if="$AUTH('systemLevel:deleted')" type="text" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
  <systemLevelTypeEdit-dialog v-if="dialog.systemLevelTypeEdit" ref="systemLevelTypeEditDialog" @success="handleSaveSuccess" @closed="dialog.systemLevelTypeEdit=false"></systemLevelTypeEdit-dialog>
  <systemLevelEdit-dialog v-if="dialog.systemLevelEdit" ref="systemLevelEditDialog" @success="handleSaveSuccess" @closed="dialog.systemLevelEdit=false"></systemLevelEdit-dialog>
</template>
<script>
  import systemAPI from '@/api/system/systemLevel';
  import systemLevelEditDialog from './systemLevelEdit';//1--导入
  import systemLevelTypeEditDialog from './systemLevelTypeEdit';//1--导入
  export default {
    name: 'systemLevel',
    components: {
        systemLevelEditDialog,systemLevelTypeEditDialog
    },
    data() {
      return {
        dialog: {
          systemLevelEdit: false,
		  systemLevelTypeEdit: false,
        },
        apiObj: systemAPI.systemLevel.initlist,
        selection: [],
        search: {
          keyword: null,
		  sysCode: null,
		  typeName: null,
          createTime: null
        }
      }
    },
    methods: {
      //添加
      add(){
        this.dialog.systemLevelEdit = true
        this.$nextTick(() => {
          this.$refs.systemLevelEditDialog.open()
        })
      },
      //编辑
      table_edit(row){
        this.dialog.systemLevelEdit = true
        this.$nextTick(() => {
          this.$refs.systemLevelEditDialog.open('edit').setData(row)
        })
      },
      //删除
      async table_del(row){
        var confirm = await this.$confirm('确定删除选中的项吗？', '提示', {
          confirmButtonText: 'ok',
          type: 'warning'
        }).catch(() => {})
        if(confirm !== 'confirm'){
          return false;
        }
        var res = await systemAPI.systemLevel.delete.delete(row.systemLevelId);
        if(res.code === 2000){
          this.$refs.table.refresh();
          this.$message.success("删除成功");
        }else{
          this.$alert(res.message, "提示", {type: 'error'});
        }
      },
	  //添加类型
	  level_type_add(systemLevelId, typeName){
	    this.dialog.systemLevelTypeEdit = true;
	    this.$nextTick(() => {
	      this.$refs.systemLevelTypeEditDialog.open('add').setDataAdd(systemLevelId, typeName);
	    });
	  },
	  //编辑类型
	  level_type_edit(row){
	    this.dialog.systemLevelTypeEdit = true;
	    this.$nextTick(() => {
	      this.$refs.systemLevelTypeEditDialog.open('edit').setData(row);
	    });
	  },
	  //删除类型
	  async level_type_del(row){
	    var confirm = await this.$confirm('确定删除选中的项吗？', '提示', {
	      confirmButtonText: 'ok',
	      type: 'warning'
	    }).catch(() => {})
	    if(confirm !== 'confirm'){
	      return false;
	    }
	    var res = await systemAPI.systemLevelType.delete.delete(row.systemLevelTypeId);
	    if(res.code === 2000){
	      this.$refs.table.refresh();
	      this.$message.success("删除成功");
	    }else{
	      this.$alert(res.message, "提示", {type: 'error'});
	    }
	  },
      //表格选择后回调事件
      selectionChange(selection){
        this.selection = selection;
      },
      //搜索
      upsearch(){
        this.$refs.table.upData(this.search);
      },
      //本地更新数据
      handleSaveSuccess(){
        this.$refs.table.refresh();
      },
      handlePermissionSuccess(){
        this.$refs.table.refresh();
      },
      handleDataSuccess(){
        this.$refs.table.refresh();
      },
      formatDate(row,column) {// 获取单元格数据
        let data = row[column.property];
        if(data == null) {
          return null;
        }
        let dt = new Date(data);
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
      },
    }
  }
</script>
<style>
</style>
